mini.parse();
var grid = mini.get("datagrid1");
var eqNum = 0; //数量
var storageNumId = "";//仓库id
var projectName = $("#projectName").val();
var form = new mini.Form("PurBarcodeListForm");
grid.setUrl(projectName+"/PurBarcodeAdmin/List");
function setData(data) {
	
	mini.get("equipmentType").setValue(data.eqType);
	mini.get("equipmentTypeName").setValue(data.eqTypeName);
	mini.get("businessKey").setValue(data.businessKey);
	mini.get("storageNumId").setValue(data.storageNumId);
	mini.get("checkBusKey").setValue(data.checkBusKey);
	eqNum = data.eqNum;
	grid.load({businessKey:data.businessKey,equipmentType:data.eqType,statusNumId:"1"});
}

function SaveDataPurBarcode() {
	var equipmentBarcodeTemp = mini.get("equipmentBarcode").getValue();
	var barcodetemp = $.trim(equipmentBarcodeTemp);
	mini.get("equipmentBarcode").setValue(barcodetemp);
	if(grid.getTotalCount()>=eqNum){
		mini.alert("条码数量已经足够");
		return;
	}
	form.validate();
	if (form.isValid() == false)
		return;
	
	if (!checkDataPurBarcode()){
		return;
		if (axtion == 'ok') {
			mini.get("equipmentBarcode").setValue("");
		}
	}

	var url = projectName+"/PurBarcodeAdmin/addPurBarcode";
	var json = mini.encode(GetDataPurBarcode());
	mini.mask({
        el: document.body,
        cls: 'mini-mask-loading',
        html: '正在保存信息...'
    }); 
	$.ajax({
		url : url,
		type : 'post',
		contentType : "application/json; charset=utf-8",
		dataType : 'json',
		data : json,
		cache : false,
		success : function(data) {
			mini.unmask();
			if (data.status == '1') {
				mini.get("equipmentBarcode").setValue("");
				grid.reload();
			} else if(data.status == '0') {
				mini.alert(data.message,'',function() {
					mini.get("equipmentBarcode").setValue("");
					mini.get("equipmentBarcode").focus();
				});
			} else {
				mini.alert("发生错误!");
			}
		},
		error : function(jqXHR, textStatus, errorThrown) {
		    mini.unmask();
			mini.alert(jqXHR.responseText);
			CloseWindowPurBarcode();
		}
	});
}

////////////////////
//标准方法接口定义
function CheckEventPurBarcode(data) {
//跨页面传递的数据对象，克隆后才可以安全使用
	data = mini.clone(data);
	if (data.action == "edit") {
		SetDataPurBarcode(data);
	} else if (data.action == "new") {
	}else if(data.action =="look"){
		SetDataPurBarcode(data);
		readOnlyForm();
	}
}

function GetDataPurBarcode(){
	var o = form.getData();
	return o;
}
function SetDataPurBarcode(data){
		$.ajax({
			url : projectName+"/PurBarcodeAdmin/queryByID?id="
					+ data.id,
			cache : false,
			success : function(text) {
				var o = mini.decode(text);
				form.setData(o);
				form.setChanged(false);
			}
			});
}
function CloseWindowPurBarcode(action) {
	if (action == "close" && form.isChanged()) {
		if (confirm("数据被修改了，是否先保存？")) {
			return false;
		}
	}
	if (window.CloseOwnerWindow)
		return window.CloseOwnerWindow(action);
	else
		window.close();
}
function onOkPurBarcode(e) {
	SaveDataPurBarcode();
}
function onCancelPurBarcode(e) {
	CloseWindowPurBarcode("cancel");
}

function labelModel() {
	var fields = form.getFields();
	for ( var i = 0, l = fields.length; i < l; i++) {
		var c = fields[i];
		if (c.setReadOnly)
			c.setReadOnly(true); //只读
		if (c.setIsValid)
			c.setIsValid(true); //去除错误提示
		if (c.addCls)
			c.addCls("asLabel"); //增加asLabel外观
	}
}
function inputModel() {
	var fields = form.getFields();
	for ( var i = 0, l = fields.length; i < l; i++) {
		var c = fields[i];
		if (c.setReadOnly)
			c.setReadOnly(false);
		if (c.removeCls)
			c.removeCls("asLabel");
	}
	mini.repaint(document.body);
}
function readOnlyForm(){
	$("#button_okPurBarcode").hide();
	$("#button_onCancelPurBarcode").hide();
	form.setEnabled(false);
}
function addRow() {
    var newRow = { name: "New Row" };
    grid.addRow(newRow, 0);

    grid.deselectAll();
    grid.select(newRow);
}

function removeRow() {
    var rows = grid.getSelecteds();
    if (rows.length > 0) {
        grid.removeRows(rows, true);
    }
}

function removeBarcode(){
	var rows = grid.getSelecteds();
    if (rows.length > 0) {
    	mini.confirm('确定删除选中记录？','确定？',function(action) {
    		if (action == 'ok') {
                var ids = [];
                for (var i = 0, l = rows.length; i < l; i++) {
                    var r = rows[i];
                    ids.push(r.id);
                }
                var idString = ids.join(',');
                
                $.ajax({
                    url: projectName+"/PurBarcodeAdmin/batchDelPurBarcode?ids=" +idString,
                    type : 'post',
                    dataType : 'json',
                    success: function (data, textStatus, jqXHR) {
                    	if (data.status == '0' || data.status == '1') {
                    		mini.alert(data.message, '', function() {
                    			mini.get("equipmentBarcode").setValue("");
    							grid.reload();
    						});
    					} else {
    						mini.alert("发生错误!", '', function() {
    							grid.reload();
    						});
    					}
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                    	mini.alert("发生错误，操作失败！");
                    }
                });
             
    		} else {
    			window.close();
    		}
    	});
    	
    } else {
        mini.alert("请选中一条记录");
    }
	
}

function checkDataPurBarcode(){
	 var falg =true; 
	var checkBusKey = mini.get("checkBusKey").getValue();
	var barcode = mini.get("equipmentBarcode").getValue();
	var equipmentType = mini.get("equipmentType").getValue();
	$.ajax({
		url : projectName+"/PurBarcodeAdmin/checkPurBarcode?businessKey="+checkBusKey+"&equipmentBarcode="+barcode+"&equipmentType="+equipmentType,
		type : 'post',
		dataType : 'json',
		async : false, 
		success : function(data) {
			 if (data.status == 0) {
				 mini.alert(data.message,'',function() {
						mini.get("equipmentBarcode").setValue("");
						mini.get("equipmentBarcode").focus();
				 });
				 falg = false;
			 }
		},
		error : function(jqXHR, textStatus, errorThrown) {
			mini.alert("调用后台验证服务异常");
			 falg = false;
		}
	});
	return falg;
}

//回车搜索
function onKeyEnter(){
	SaveDataPurBarcode();
}

function onBarCodeValidation(e) {
    if (e.isValid) {
     var barcodetemp = $.trim(e.value);
        if (!((barcodetemp).length == 13||(barcodetemp).length == 14)) {
            e.errorText = "必须输入13位或14位设备编码";
            e.isValid = false;
        }
    }
}

function exportPurBarcode(){
	var rows = grid.getSelecteds();
	var idString = '';
	var url = projectName +"/PurBarcodeAdmin/exportPurBarcodeExcel?";
    if (rows.length > 0) {
	    var ids = [];
	    for (var i = 0, l = rows.length; i < l; i++) {
	        var r = rows[i];
	        ids.push(r.id);
	    }
	    idString = ids.join(',');    
	    mini.get("ids").setValue(idString);
	    util.exportDataToClient($('#PurBarcodeListForm'), url);
	} else {
		
		mini.confirm('导出全部？如查询条件不为空，则导出符合查询条件的所有数据.','确定？',function(action) {
			if (action == 'ok') {
				mini.get('ids').setValue('');
				util.exportDataToClient($('#PurBarcodeListForm'), url);
			} else {
				window.close();
			}
		});
	}
}